package com.epalmpay.service.bonus.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.bonus.BonusShopSettleLogInDTO;
import com.epalmpay.dto.bonus.BonusShopSettleLogListOutDTO;
import com.epalmpay.dto.bonus.BonusShopSettleLogOutDTO;
import com.epalmpay.dto.bonus.BonusVerifyLogInDTO;
import com.epalmpay.dto.bonus.BonusVerifyLogListOutDTO;
import com.epalmpay.dto.bonus.BonusVerifyLogOutDTO;
import com.epalmpay.dto.bonus.BonusVerifyShopInDTO;
import com.epalmpay.dto.bonus.BonusVerifyShopListOutDTO;
import com.epalmpay.dto.bonus.BonusVerifyShopOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.system.ShopListOutDTO;
import com.epalmpay.dto.system.ShopOutDTO;
import com.epalmpay.entity.BonusShopSettleLog;
import com.epalmpay.entity.BonusVerifyLog;
import com.epalmpay.entity.BonusVerifyShop;
import com.epalmpay.entity.Industry;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.Shop;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.mapper.BonusShopSettleLogMapper;
import com.epalmpay.mapper.BonusVerifyLogMapper;
import com.epalmpay.mapper.BonusVerifyShopMapper;
import com.epalmpay.mapper.ShopMapper;
import com.epalmpay.service.bonus.IBonusVerifyShopService;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Tools;



@Service("bonusVerifyShopService")
public class BonusVerifyShopImpl implements IBonusVerifyShopService {
	

	@Resource
	private BonusVerifyShopMapper bonusVerifyShopMapper;
	@Resource
	private BonusShopSettleLogMapper bonusShopSettleLogMapper;
	@Resource
	private ShopMapper shopMapper;
	@Resource
	private BonusVerifyLogMapper bonusVerifyLogMapper;

	@Override
	public BonusVerifyShopListOutDTO listBonusVerifyShop(BonusVerifyShopInDTO bonusVerifyShopInDTO, PageDTO pageDTO)
			throws ApplicationException {
		BonusVerifyShop bonusVerifyShop=new BonusVerifyShop();
		Page page = Tools.pageEmtity(pageDTO);
		bonusVerifyShop.setPage(page); 
		bonusVerifyShop.setGroupId(bonusVerifyShopInDTO.getGroupId());
		bonusVerifyShop.setStatus(bonusVerifyShopInDTO.getStatus());
		bonusVerifyShop.setJoinStart(bonusVerifyShopInDTO.getJoinStart());
		bonusVerifyShop.setJoinEnd(bonusVerifyShopInDTO.getJoinEnd());
		Industry industryType=new Industry();
		industryType.setId(bonusVerifyShopInDTO.getIndustryId()); 
		bonusVerifyShop.setIndustryType(industryType);
		Shop shop=new Shop();
		shop.setMerchantName(bonusVerifyShopInDTO.getMerchantName());
		bonusVerifyShop.setShop(shop); 
		List<BonusVerifyShop> list=bonusVerifyShopMapper.bonusVerifyShoplistPage(bonusVerifyShop);
		List<BonusVerifyShopOutDTO> bonusList=new ArrayList<BonusVerifyShopOutDTO>();
		BonusVerifyShopListOutDTO bonusVerifyShopListOutDTO = new BonusVerifyShopListOutDTO();
		for (BonusVerifyShop bonus : list) {
			try {
				BonusVerifyShopOutDTO bonusVerifyShopOutDTO=new BonusVerifyShopOutDTO();
				PropertyCopyUtil.copyPropertiesExclude(bonus, bonusVerifyShopOutDTO);
				bonusVerifyShopOutDTO.setIndustryName(bonus.getIndustryType().getName());
				bonusVerifyShopOutDTO.setShopName(bonus.getShop().getMerchantName());

				//现在有结算比例 所以不能从BonusVerifyShop查询核销几分熟和本月核销积分点数
				BonusVerifyLog bonusVerifyLog = bonusVerifyLogMapper.queryShopBonusVerifyLogInfo(bonus.getShopId());
				if(bonusVerifyLog !=null){
					bonusVerifyShopOutDTO.setVerifiedAmount(bonusVerifyLog.getAdjustAmount());
				}else{
					bonusVerifyShopOutDTO.setVerifiedAmount(0);
				}
				BonusVerifyLog bonusVerifyLog2 = bonusVerifyLogMapper.queryThisMonthShopBonusVerifyLogInfo(bonus.getShopId(),new Date());
				if(bonusVerifyLog2 !=null){
					bonusVerifyShopOutDTO.setThisAmount(bonusVerifyLog2.getAdjustAmount());
				}else{
					bonusVerifyShopOutDTO.setThisAmount(0);
				}

				bonusList.add(bonusVerifyShopOutDTO);
				bonusVerifyShopListOutDTO.setBonusVerifyShopOutDTO(bonusList);
			} catch (Exception e) {
				throw new ApplicationException(Rescode.FAIL,"数据拷贝异常");
			}
			
		}
		bonusVerifyShopListOutDTO.setPage(page);
		bonusVerifyShopListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return bonusVerifyShopListOutDTO;
	}

	@Override
	public ShopListOutDTO listCanShop(BonusVerifyShopInDTO bonusVerifyShopInDTO, PageDTO pageDTO) throws ApplicationException {
		Page page = Tools.pageEmtity(pageDTO);
		Shop shop=new Shop();
		shop.setPage(page);
		shop.setGroupId(bonusVerifyShopInDTO.getGroupId());
		if(bonusVerifyShopInDTO.getIndustryId()!=null){
			shop.setIndustry(bonusVerifyShopInDTO.getIndustryId());
		}
		if(bonusVerifyShopInDTO.getMerchantName()!=null){
			shop.setMerchantName(bonusVerifyShopInDTO.getMerchantName());
		}
		List<Shop>  shoplist=shopMapper.canShopBonuslistPage(shop);
		ShopListOutDTO shopListOutDTO=new ShopListOutDTO();
		List<ShopOutDTO> shopOutList=new ArrayList<ShopOutDTO>();
		for (Shop shopDto : shoplist) {
			ShopOutDTO shopOutDTO=new ShopOutDTO();
			shopOutDTO.setId(shopDto.getId());
			shopOutDTO.setMerchantName(shopDto.getMerchantName());
			shopOutDTO.setIndustryName(shopDto.getIndustryType().getName());
			shopOutList.add(shopOutDTO);
			shopListOutDTO.setShopOutDTOList(shopOutList); 
		}
		shopListOutDTO.setPage(page);
		shopListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return shopListOutDTO;
	}

	@Override
	public Boolean addBonusVerifyShop(BonusVerifyShopInDTO bonusVerifyShopInDTO) throws ApplicationException {
		String shopIds = bonusVerifyShopInDTO.getShopIds();
		if(null != shopIds && !"".equals(shopIds)){
		String shopId[] = shopIds.split(","); 
		for (int i = 0; i < shopId.length; i++) {
			BonusVerifyShop bonusVerifyShop=new BonusVerifyShop();
			bonusVerifyShop.setGmtCreate(new Date());
			bonusVerifyShop.setGmtModified(new Date());
			bonusVerifyShop.setGroupId(bonusVerifyShopInDTO.getGroupId());
			bonusVerifyShop.setAmount(0);
			bonusVerifyShop.setVerifiedAmount(0);
			bonusVerifyShop.setThisAmount(0);
			bonusVerifyShop.setLastSettleTime(null);
			bonusVerifyShop.setStatus(1);
			bonusVerifyShop.setShopId(Long.valueOf(shopId[i])); 
			bonusVerifyShopMapper.insert(bonusVerifyShop);
		} 
		return true;
	}else{
		return false;
		}
		
	}

	@Override
	public Boolean updateBonusStatus(BonusVerifyShopInDTO bonusVerifyShopInDTO) throws ApplicationException {
		BonusVerifyShop bonusVerifyShop=bonusVerifyShopMapper.selectByPrimaryKey(bonusVerifyShopInDTO.getId());
		bonusVerifyShop.setStatus(bonusVerifyShopInDTO.getStatus());
		bonusVerifyShopMapper.updateByPrimaryKey(bonusVerifyShop);
		return true;
	} 

	@Override
	public Boolean updateBonusSettlement(BonusShopSettleLogInDTO bonusShopSettleLogInDTO) throws ApplicationException {
		BonusVerifyShop bonusVerifyShop=bonusVerifyShopMapper.selectByPrimaryKey(bonusShopSettleLogInDTO.getBonusVerifyShopId());
		if(bonusVerifyShop==null){
			throw new ApplicationException(Rescode.FAIL,"未查询到可核销门店信息");
		}
		Date date=new Date();
		Integer settle_balance=bonusVerifyShop.getAmount()-bonusShopSettleLogInDTO.getSettleBonus();
		if(settle_balance<0){
			throw new ApplicationException(Rescode.FAIL,"积分可用余额不足结算");
		}
		bonusVerifyShop.setAmount(settle_balance);
		bonusVerifyShop.setLastSettleTime(date);
		bonusVerifyShopMapper.updateByPrimaryKey(bonusVerifyShop);
		BonusShopSettleLog bonusShopSettleLog=new BonusShopSettleLog();
		bonusShopSettleLog.setGmtCreate(date);
		bonusShopSettleLog.setGmtModified(date); 
		bonusShopSettleLog.setGroupId(bonusVerifyShop.getGroupId());
		bonusShopSettleLog.setShopId(bonusVerifyShop.getShopId());
		bonusShopSettleLog.setMemo(bonusShopSettleLogInDTO.getMemo());
		bonusShopSettleLog.setOperator(bonusShopSettleLogInDTO.getOperator());
		bonusShopSettleLog.setSettleBalance(settle_balance);  
		bonusShopSettleLog.setSettleAmount(bonusShopSettleLogInDTO.getSettleBonus());
		bonusShopSettleLogMapper.insert(bonusShopSettleLog);
		return true;
	}

	@Override
	public BonusVerifyShopOutDTO BonusVerifyShopView(BonusVerifyShopInDTO bonusVerifyShopInDTO)
			throws ApplicationException {
		BonusVerifyShopOutDTO bonusVerifyShopOutDTO=new BonusVerifyShopOutDTO();
		BonusVerifyShop bonusVerifyShop=bonusVerifyShopMapper.selectBonusVerifyShopInfo(bonusVerifyShopInDTO.getId());
		try {
			PropertyCopyUtil.copyPropertiesExclude(bonusVerifyShop, bonusVerifyShopOutDTO);
			bonusVerifyShopOutDTO.setIndustryName(bonusVerifyShop.getIndustryType().getName());
			bonusVerifyShopOutDTO.setShopName(bonusVerifyShop.getShop().getMerchantName());
			//现在有结算比例 所以不能从BonusVerifyShop查询核销几分熟和本月核销积分点数
			BonusVerifyLog bonusVerifyLog = bonusVerifyLogMapper.queryShopBonusVerifyLogInfo(bonusVerifyShopInDTO.getShopId());
			if(bonusVerifyLog !=null){
				bonusVerifyShopOutDTO.setVerifiedAmount(bonusVerifyLog.getAdjustAmount());
			}else{
				bonusVerifyShopOutDTO.setVerifiedAmount(0);
			}

			bonusVerifyShopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return bonusVerifyShopOutDTO;
		} catch (Exception e) {
			throw new ApplicationException(Rescode.FAIL,"数据拷贝异常");
		}
	}

	
	
	@Override
	public BonusVerifyLogListOutDTO listMonthBonusVerifyLog(BonusVerifyLogInDTO bonusVerifyShopInDTO, PageDTO pageDTO)
			throws ApplicationException {
		BonusVerifyLog bonusVerifyLog=new BonusVerifyLog();
		Page page = Tools.pageEmtity(pageDTO);
		bonusVerifyLog.setPage(page);
		bonusVerifyLog.setShopId(bonusVerifyShopInDTO.getShopId());
		bonusVerifyLog.setAdjustStatus(ActivityEnum.BonusVerityStatus.OutAccounted.getType());
		List<BonusVerifyLog> bonusVerifyLogList=bonusVerifyLogMapper.bonusMonthLoglistPage(bonusVerifyLog);
		BonusVerifyLogListOutDTO bonusVerifyLogListOutDTO=new BonusVerifyLogListOutDTO();
		List<BonusVerifyLogOutDTO> bonusVerifyLogOutDTO=new ArrayList<BonusVerifyLogOutDTO>();
		for (BonusVerifyLog bv : bonusVerifyLogList) {
			BonusVerifyLogOutDTO bonusLog=new BonusVerifyLogOutDTO();
			bonusLog.setId(bv.getId());
			bonusLog.setMemo(bv.getMemo()); 
			bonusLog.setAdjustAmount(bv.getAdjustAmount()==null?0:bv.getAdjustAmount());
			bonusLog.setAdjustValue(bv.getAdjustValue()==null?0:bv.getAdjustValue());
			bonusLog.setStatistics(bv.getStatistics()); 
			bonusVerifyLogOutDTO.add(bonusLog);
			bonusVerifyLogListOutDTO.setBonusVerifyLogOutDTOList(bonusVerifyLogOutDTO); 
		}
		bonusVerifyLogListOutDTO.setPage(page);
		bonusVerifyLogListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return bonusVerifyLogListOutDTO; 
		
	}
	
	
	@Override
	public BonusVerifyLogListOutDTO listBonusVerifyLog(BonusVerifyLogInDTO bonusVerifyShopInDTO, PageDTO pageDTO)
			throws ApplicationException {
		BonusVerifyLog bonusVerifyLog=new BonusVerifyLog();
		Page page = Tools.pageEmtity(pageDTO);
		bonusVerifyLog.setPage(page);
		bonusVerifyLog.setShopId(bonusVerifyShopInDTO.getShopId());
		bonusVerifyLog.setStatistics(bonusVerifyShopInDTO.getStatistics()); 
		//不查询锁定积分
		bonusVerifyLog.setAdjustStatus(ActivityEnum.BonusVerityStatus.Locked.getType()); 
		List<BonusVerifyLog> bonusVerifyLogList=bonusVerifyLogMapper.bonusVerifyLoglistPage(bonusVerifyLog);
		BonusVerifyLogListOutDTO bonusVerifyLogListOutDTO=new BonusVerifyLogListOutDTO();
		List<BonusVerifyLogOutDTO> bsList=new ArrayList<BonusVerifyLogOutDTO>();
		for (BonusVerifyLog log : bonusVerifyLogList) {
			BonusVerifyLogOutDTO bonusLog=new BonusVerifyLogOutDTO();
			try {
				bonusLog.setId(log.getId());
				bonusLog.setTradeOfflineId(log.getTradeOffline().getOrderId()); 
				bonusLog.setAdjustStatus(log.getAdjustStatus()); 
				bonusLog.setMemberName(log.getMember().getName()); 
				bonusLog.setOrderNo(log.getOrderId()+""); 
				bonusLog.setGmtCreate(log.getGmtCreate());
				bonusLog.setAdjustAmount(log.getAdjustAmount());
				bonusLog.setSettingAmount(log.getSettingAmount());
				bsList.add(bonusLog);
				bonusVerifyLogListOutDTO.setBonusVerifyLogOutDTOList(bsList); 
			} catch (Exception e) {
				throw new ApplicationException(Rescode.FAIL,"数据拷贝异常");
			}
		} 
		bonusVerifyLogListOutDTO.setPage(page);
		bonusVerifyLogListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return bonusVerifyLogListOutDTO;
	}
	

	@Override
	public BonusShopSettleLogListOutDTO listBonusShopSettleLog(BonusShopSettleLogInDTO bonusShopSettleLogInDTO,
			PageDTO pageDTO) throws ApplicationException {
		BonusShopSettleLog bonusShopSettleLog=new BonusShopSettleLog();
		Page page = Tools.pageEmtity(pageDTO);
		bonusShopSettleLog.setPage(page);
		bonusShopSettleLog.setShopId(bonusShopSettleLogInDTO.getShopId());
		bonusShopSettleLog.setStatistics(bonusShopSettleLogInDTO.getStatistics());
		List<BonusShopSettleLog> BonusShopSettleLogList=bonusShopSettleLogMapper.bonusShopSettleLoglistPage(bonusShopSettleLog);
		BonusShopSettleLogListOutDTO bonusShopSettleLogListOutDTO=new BonusShopSettleLogListOutDTO();
		List<BonusShopSettleLogOutDTO> bsList=new ArrayList<BonusShopSettleLogOutDTO>();
		for (BonusShopSettleLog bonusShopSettleLog2 : BonusShopSettleLogList) {
			BonusShopSettleLogOutDTO bs=new BonusShopSettleLogOutDTO();
			try {
				PropertyCopyUtil.copyPropertiesExclude(bonusShopSettleLog2,bs);
				bs.setUserName(bonusShopSettleLog2.getUserbase().getUsername());
				bsList.add(bs);
				bonusShopSettleLogListOutDTO.setBonusShopSettleLogOutDTO(bsList);
			} catch (Exception e) {
				throw new ApplicationException(Rescode.FAIL,"数据拷贝异常");
			}
		}
		bonusShopSettleLogListOutDTO.setPage(page);
		bonusShopSettleLogListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return bonusShopSettleLogListOutDTO; 
		 
	}
	@Override
	public BonusVerifyShopOutDTO bonusMonthVerifyShopView(BonusVerifyShopInDTO bonusVerifyShopInDTO)
			throws ApplicationException {
		BonusVerifyShopOutDTO bonusVerifyShopOutDTO=new BonusVerifyShopOutDTO();
		BonusVerifyShop bonusVerifyShop=bonusVerifyShopMapper.bonusMonthVerifyShopView(bonusVerifyShopInDTO.getId(),bonusVerifyShopInDTO.getStatistics());
//		BonusVerifyLog bonusVerifyLog =bonusVerifyLogMapper.queryMonthShopBonusVerifyInfo(bonusVerifyShopInDTO.getId(),bonusVerifyShopInDTO.getStatistics()+"01")

		try {
			PropertyCopyUtil.copyPropertiesExclude(bonusVerifyShop, bonusVerifyShopOutDTO);
			bonusVerifyShopOutDTO.setIndustryName(bonusVerifyShop.getIndustryType().getName());
			bonusVerifyShopOutDTO.setShopName(bonusVerifyShop.getShop().getMerchantName());
			bonusVerifyShopOutDTO.setTotalCount(bonusVerifyShop.getTotalCount());

			BonusVerifyLog bonusVerifyLog = bonusVerifyLogMapper.queryShopBonusVerifyLogInfo(bonusVerifyShopInDTO.getShopId());
			if(bonusVerifyLog !=null){
				bonusVerifyShopOutDTO.setVerifiedAmount(bonusVerifyLog.getAdjustAmount());
			}else{
				bonusVerifyShopOutDTO.setVerifiedAmount(0);
			}


			bonusVerifyShopOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return bonusVerifyShopOutDTO;
		} catch (Exception e) {
			throw new ApplicationException(Rescode.FAIL,"数据拷贝异常");
		}
		
	}

	

}
